package com.zeng.sim.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zeng.sim.controller.domain.SysMenu;
import com.zeng.sim.controller.vo.SysMenuVo;
import com.zeng.sim.mapper.SysMenuMapper;
import com.zeng.sim.mapper.model.SysMenuEntity;
import com.zeng.sim.service.SysMenuService;
import com.zeng.sim.util.SimConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zengwanlin
 * @since 2023-12-20
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuEntity> implements SysMenuService {

    /**
     * 递归获取菜单tree
     * @param sysMenus
     * @param parentId
     * @return
     */
    @Override
    public List<SysMenuVo> toUserMenu(List<SysMenuVo> sysMenus, String parentId) {
        List<SysMenuVo> list = new ArrayList<>();
        for (SysMenuVo menu : sysMenus) {
            if (parentId.equals(menu.getParentId())) {
                menu.setChildren(toUserMenu(sysMenus, menu.getId()));
                list.add(menu);
            }
        }
        return list;
    }

    @Override
    public boolean valid(String resId) {
        SysMenuEntity entity = new SysMenuEntity();
        entity.setResId(resId);
        entity.setValidFlag(SimConstant.VALID_FLAG_1);
        return updateById(entity);
    }

    @Override
    public boolean invalid(String resId) {
        SysMenuEntity entity = new SysMenuEntity();
        entity.setResId(resId);
        entity.setValidFlag(SimConstant.VALID_FLAG_0);
        return updateById(entity);
    }

    @Override
    public List<SysMenu> list(SysMenu sysMenu) {
        LambdaQueryWrapper<SysMenuEntity> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(sysMenu.getResCode())) {
            wrapper.eq(SysMenuEntity::getResCode, sysMenu.getResCode());
        }
        if (StringUtils.hasLength(sysMenu.getResName())) {
            wrapper.like(SysMenuEntity::getResName, sysMenu.getResName());
        }
        if (StringUtils.hasLength(sysMenu.getResType())) {
            wrapper.eq(SysMenuEntity::getResType, sysMenu.getResType());
        }
        List<SysMenuEntity> menuEntityList = list(wrapper);
        List<SysMenu> list = new ArrayList<>();
        for (SysMenuEntity menuEntity : menuEntityList) {
            SysMenu menu = new SysMenu();
            BeanUtils.copyProperties(menuEntity, menu);
            list.add(menu);
        }
        return list;
    }

    @Override
    public boolean update(SysMenu sysMenu) {
        SysMenuEntity entity = new SysMenuEntity();
        entity.setResId(sysMenu.getResId());
        entity.setResName(sysMenu.getResName());
        entity.setResType(sysMenu.getResType());
        entity.setResCode(sysMenu.getResCode());
        entity.setResUrl(sysMenu.getResUrl());
        entity.setOpenType(sysMenu.getOpenType());
        entity.setParentId(sysMenu.getParentId());
        entity.setIcon(sysMenu.getIcon());
        entity.setSort(sysMenu.getSort());
        entity.setRemark(sysMenu.getRemark());
        entity.setValidFlag(sysMenu.getValidFlag());
        return updateById(entity);
    }

    @Override
    public boolean remove(List<String> resIdList) {
        LambdaQueryWrapper<SysMenuEntity> wrapper = new LambdaQueryWrapper<>();
        for (String resId : resIdList) {
            wrapper.clear();
            wrapper.eq(SysMenuEntity::getParentId, resId);
            if (exists(wrapper)) {
                throw new IllegalArgumentException("存在子权限，不能删除");
            }
        }
        return removeBatchByIds(resIdList);
    }

}
